package cn.nexd.app.perimission;

import android.support.v4.app.ActivityCompat;

import java.lang.ref.WeakReference;

import cn.nexd.app.activity.LoadingActivity;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;

/**
 * 处理Loading页面的运行时权限问题
 * <p>
 * Loading页面申请定位权限和文件的写入读入权限
 * <p>
 * Created by wangxu on 16/4/1.
 */
public class LoadingActivityPermissionsDispatcher {
    private static final int REQUEST_STORAGE = 0;
    private static final int REQUEST_LOCATION = 1;
    private static final int REQUEST_PHONE = 2;

    private static final String[] PERMISSION_STORAGE = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"};
    private static final String[] PERMISSION_LOCATION = new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};
    private static final String[] PERMISSION_PHONE = new String[]{"android.permission.READ_PHONE_STATE"};

    private LoadingActivityPermissionsDispatcher() {
    }

    public static void showPhoneWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_PHONE)) {
            target.uploadOperaRecord();
        } else {
            ActivityCompat.requestPermissions(target, PERMISSION_PHONE, REQUEST_PHONE);
        }
    }

    public static void showStorgeWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_STORAGE)) {
            target.checkPhonePermission();
        } else {
            ActivityCompat.requestPermissions(target, PERMISSION_STORAGE, REQUEST_STORAGE);
        }
    }

    public static void showLocationWithCheck(LoadingActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, PERMISSION_LOCATION)) {
            target.locationCurrentPermission();
        } else {
            ActivityCompat.requestPermissions(target, PERMISSION_LOCATION, REQUEST_LOCATION);
        }
    }


    public static void onRequestPermissionsResult(LoadingActivity target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_STORAGE:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_STORAGE)) {
                    target.onStorageDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.checkPhonePermission();
                } else {
                    target.onStorageDenied();
                }
                break;

            case REQUEST_LOCATION:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_LOCATION)) {
                    target.onLocationDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.locationCurrentPermission();
                } else {
                    target.onLocationDenied();
                }
                break;
            case REQUEST_PHONE:
                if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_PHONE)) {
                    target.onPhoneDenied();
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    target.uploadOperaRecord();
                } else {
                    target.onPhoneDenied();
                }
                break;
            default:
                break;
        }
    }

    public static final class ShowPhonePermissionRequest implements PermissionRequest {

        private final WeakReference<LoadingActivity> weakReference;

        public ShowPhonePermissionRequest(LoadingActivity target) {
            this.weakReference = new WeakReference<LoadingActivity>(target);
        }

        @Override
        public void proceed() {
            LoadingActivity target = weakReference.get();
            if (target == null) return;
            ActivityCompat.requestPermissions(target, PERMISSION_PHONE, REQUEST_PHONE);
        }

        @Override
        public void cancel() {
            LoadingActivity target = weakReference.get();
            if (target == null) return;
            target.onPhoneDenied();
        }
    }

}
